package com.socket.UDP.Thread;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * Created by wangyg on 2016/10/31.
 */
public class UDPThread extends Thread {
	private DatagramSocket socket;
	private DatagramPacket packet;
	private byte[] data;

	public UDPThread(DatagramSocket socket, DatagramPacket packet, byte[] data) {
		this.socket = socket;
		this.packet = packet;
		this.data = data;
	}



	@Override
	public void run() {
		try {

			//读取数据
			String info = new String(data, 0, packet.getLength());
			System.out.println("服务器接收成功，消息为：" + info);

			//响应请求
			InetAddress address = packet.getAddress();
			int port = packet.getPort();
			byte[] sendData = "欢迎你的到来。".getBytes();
			DatagramPacket packet1 = new DatagramPacket(sendData, sendData.length, address, port);
			//发送信息
			socket.send(packet1);
			//关闭连接
//			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
